bookApp.controller('UserController', function ($scope, $rootScope, $http, $filter, ngTableParams, $resource, $modal) {

    $scope.paginationConf={
        currentPage:1,
        totalItems:0,
        itemsPerPage: 30
    };

    $scope.userTableParams = new ngTableParams({
        page: 1,            // show first page
        count: 100
    },{
        total: 0,
        counts:[],
        getData: function ($defer, params) {
            $http.get("/api/user").success(function(data){
                if(data.code == 200){
                    $scope.users = data.data.result;
                    $scope.paginationConf.totalItems = data.data.totalCount;
                    $defer.resolve($scope.users);
                }
            });
        }
    });

    $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', function () {
        $scope.userTableParams.reload();
    });

    $scope.openAddUserModal = function(){
        var modalInstance = $modal.open({
            templateUrl : 'userModal.html',
            controller : 'UserModalCtrl',
            size : 'md',
            resolve : {
                user : function(){
                    return {};
                }
            }
        });
        modalInstance.result.then(function () {
            $scope.userTableParams.reload();
        },function () {
            $scope.userTableParams.reload();
        });
    }

    $scope.openEditUserModel = function (user) {
        var modalInstance = $modal.open({
            templateUrl : 'userModal.html',
            controller : 'UserModalCtrl',
            size : 'md',
            resolve : {
                user : function(){
                    return user;
                }
            }
        });
        modalInstance.result.then(function () {
            $scope.userTableParams.reload();
        },function () {
            $scope.userTableParams.reload();
        });
    }

    $scope.openDelUserModal = function (user) {
        var modalInstance = $modal.open({
            templateUrl : 'delUserModal.html',
            controller : 'DelUserModalCtrl',
            size : 'md',
            resolve : {
                user : function(){
                    return user;
                }
            }
        });
        modalInstance.result.then(function () {
            $scope.userTableParams.reload();
        },function () {
            $scope.userTableParams.reload();
        });
    }

});

bookApp.controller('UserModalCtrl', function ($scope, $rootScope, $http, $filter,$resource, $modalInstance,user) {
    $scope.user = angular.copy(user);
    $scope.save = function () {
        if($scope.user.id){
            $http.put("/api/user",{id:$scope.user.id,username:$scope.user.username,name:$scope.user.name,password:$scope.user.password}).success(function (data) {
                $modalInstance.close();
            }).error(function (data) {
                $scope.errorMsg = "操作失败，请填写正确的用户信息"
            });
        }else{
            $http.post("/api/user",$scope.user).success(function (data) {
                $modalInstance.close();
            }).error(function (data) {
                $scope.errorMsg = "操作失败，请填写正确的用户信息"
            });
        }
    }

    $scope.cancel = function () {
        $modalInstance.dismiss();
    }

});
bookApp.controller('DelUserModalCtrl', function ($scope, $rootScope, $http, $filter,$resource, $modalInstance,user) {
    $scope.user = angular.copy(user);
    $scope.save = function () {
        $http.delete("/api/user/"+$scope.user.id).success(function () {
            $modalInstance.close();
        }).error(function (data) {
            $scope.errorMsg = "删除操作失败！";
        });
    }

    $scope.cancel = function(){
        $modalInstance.dismiss();
    }
});

